package main

import (
	"encoding/xml"
	"fmt"
	"strings"
)

//XML文件格式的文件的编码和解码
var (
	t, token xml.Token
)
var err error

//这里是xml文件格式的解码
func main() {
	input := "<Person><FirstName>Laura</FirstName><LastName>Lynn</LastName></Person>"
	inputReader := strings.NewReader(input)
	p := xml.NewDecoder(inputReader) //解码流
	for t, err = p.Token(); err == nil; t, err = p.Token() {
		switch token := t.(type) {
		case xml.StartElement:
			name := token.Name.Local
			fmt.Printf("Token name is %s", name)
			for _, attr := range token.Attr {
				attrName := attr.Name.Local
				attrValue := attr.Value
				fmt.Printf("An attribute is: %s %s", attrName, attrValue)
			}

		case xml.EndElement:
			fmt.Println("End of token")
		case xml.CharData:
			content := string([]byte(token))
			fmt.Println("", content)
		}
	}

}
